package com.rslai.base.tool.servertest.command;

import com.rslai.base.tool.servertest.context.Context;
import com.rslai.base.tool.servertest.exception.CommandNotFoundException;
import com.rslai.base.tool.servertest.response.Response;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TransformStepCommand extends StepCommand {
    Transform ts;

    public TransformStepCommand(Transform ts) {
        this.ts = ts;
    }

    public Response doExecute(Response result, Context context) throws Throwable {
        if (this.ts == null) {
            throw new CommandNotFoundException("transform command not founded.");
        }
        if (result == null) {
            result = new Response();
            result.setBody(this.ts.transport(null));
        } else {
            result.setBody(this.ts.transport(result.getBody()));
        }
        return result;
    }

    public StepCommand doClone() {
        return new TransformStepCommand(this.ts);
    }

    public Map<String, Object> toReport() {
        Map details = new HashMap();
        details.put("stepName", "执行转换器:");
        details.put("name", this.ts.getClass().getCanonicalName());
        List params = new ArrayList();
        details.put("params", params);
        return details;
    }
}
